﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Usermanagment.VisualDisplay
{
	using Usermanagment.DBConnection;
	
	public partial class Createscreen : Form
	{
		private DBConnect dbc;
		private UserCreate uc;
		private DBDatafetch dft;
		public Createscreen()
		{
			InitializeComponent();
			this.MaximumSize = this.MinimumSize = this.Size;
			//button1.Visible = false;
			textBox1.Enabled = false;
		}

		public Createscreen(DBConnect dbc)
			: this()
		{
			this.dbc = dbc;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (fieldsNEmpt())
			{
				update();
				Dispose();
			}
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			if (fieldsNEmpt())
			{
				update();
				if (textBox1.Text.Equals("Added"))
				{
					Username.Text = "";
					Password.Text = "";
				}
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			Dispose();
		}

		private void update()
		{
			if (dbc.Connected())
			{
				uc = new UserCreate(dbc);
				dft = new DBDatafetch(dbc);
				
				int id = dft.getID(Username.Text);
				if (id <= 0)
				{
					uc.insertUser(Username.Text, Password.Text);
					textBox1.Text = uc.getStat();
				}
				else
					textBox1.Text = "User already Exists";
			}
		}

		private Boolean fieldsNEmpt()
		{
			return (Username.Text.Length > 4 && Password.Text.Length > 4);
		}

		private void button1_Click(object sender, EventArgs e)
		{
			dft = new DBDatafetch(dbc);
			textBox1.Text = dft.fetchPass(Username.Text)+"";
		}

		
	}
}
